package member

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
	"go-papers/tools/app/msg"
)

func GetMemberStudentList(c *gin.Context) {
	var data models.MemberStudent
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("studentId"); index != "" {
		data.StudentId = tools.StrToInt(err, index)
	}
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}
	data.StudentName = c.Request.FormValue("studentName")
	data.StudentDay = c.Request.FormValue("studentDay")
	data.StudentAge = c.Request.FormValue("studentAge")
	data.StudentPhone = c.Request.FormValue("studentPhone")
	data.StudentGrade = c.Request.FormValue("studentGrade")
	data.Province = c.Request.FormValue("province")
	data.City = c.Request.FormValue("city")
	data.Area = c.Request.FormValue("area")
	data.Address = c.Request.FormValue("address")

	result, count, err := data.GetSearchPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetMemberStudentListAll(c *gin.Context) {
	var data models.MemberStudent
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("studentId"); index != "" {
		data.StudentId = tools.StrToInt(err, index)
	}
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}
	data.StudentName = c.Request.FormValue("studentName")
	data.StudentDay = c.Request.FormValue("studentDay")
	data.StudentAge = c.Request.FormValue("studentAge")
	data.StudentPhone = c.Request.FormValue("studentPhone")
	data.StudentGrade = c.Request.FormValue("studentGrade")
	data.Province = c.Request.FormValue("province")
	data.City = c.Request.FormValue("city")
	data.Area = c.Request.FormValue("area")
	data.Address = c.Request.FormValue("address")

	data.DataScope = tools.GetUserIdStr(c)
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetMemberStudent(c *gin.Context) {
	var data models.MemberStudent

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}
	id := c.Request.FormValue("studentId")
	data.StudentId, _ = tools.StringToInt(id)
	if data.StudentId == 0 {
		app.CommonResp(c, -1, "学生信息未获取到："+id, nil)
		return
	}
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func InsertMemberStudent(c *gin.Context) {
	var data models.MemberStudent
	err := c.ShouldBindJSON(&data)

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}
	//data.CreateBy = tools.GetUserIdStr(c)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	result, err := data.Create()
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func UpdateMemberStudent(c *gin.Context) {
	var data models.MemberStudent
	err := c.BindWith(&data, binding.JSON)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}

	result, err := data.Update(data.StudentId)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func GetDefaultStudent(c *gin.Context) {
	var data models.MemberStudent

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}
	data.IsDefault = "1"
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "成功")

}

// 设置学生为 默认学生
func DefaultStudent(c *gin.Context) {

	var data models.MemberStudent
	ss := c.Request.FormValue("studentId")
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}

	_, err := data.UpdateUserDefault(ss)
	if err != nil {
		app.CommonResp(c, -1, "操作失败"+err.Error(), nil)
		return
	}
	app.CommonResp(c, 0, "操作成功", nil)
	return
}

func DeleteMemberStudent(c *gin.Context) {
	var data models.MemberStudent
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "请登陆后操作", nil)
		return
	}
	IDS := tools.IdsStrToIdsIntGroupV3("studentId", c)
	_, err := data.BatchDelete(IDS)
	if err != nil {
		app.ApiResp(c, -1, "数据删除失败："+err.Error(), nil)
		return
	}

	app.OK(c, nil, msg.DeletedSuccess)
}
